#encoding:utf-8
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from szdb.models import User, Activity, News, NewsType
from szdb.adjtime import getDatetime, getNow
#import time
#from django.utils import timezone
#from django.conf import settings
#import datetime


class UserTest(TestCase):
    def setUp(self):
        self.user = User()
        # 基础用户建立
        self.baseuser = User(username='zdz',
                             password='12345',
                             email='zdz@126.com',
                             type=1,
                             safequestion='我的手机号码',
                             safeanswer='13805728737',
                             sex=0,
                             )
        self.baseuser.save()
        # 基础活动建立
        self.acti = Activity(title='腐败一日游',
                             content='逛西湖一天',
                             releasetime=getNow(),
                             date=getNow(),
                             owner=self.baseuser,
                             place='杭州西湖',
                             )
        self.acti.save()
    
    # 返回用户名是否可用
    def test_is_username_unique(self):
        # Exception
        self.assertRaises(KeyError, self.user.is_username_unique, {})
        self.assertEqual(False, self.user.is_username_unique({'username':self.baseuser.username}))
        self.assertEqual(True, self.user.is_username_unique({'username':self.baseuser.username + '123'}))
        
    # 返回邮箱是否可用
    def test_is_email_unique(self):
        # Exception
        self.assertRaises(KeyError, self.user.is_email_unique, {})
        self.assertEqual(False, self.user.is_email_unique({'email':self.baseuser.email}))
        self.assertEqual(True, self.user.is_email_unique({'email':self.baseuser.email + '123'}))
        
    
    # 注册
    def test_register(self):
        # Exception
        self.assertRaises(KeyError, self.user.register, {})
        # 正确数据
        new_data = {'username':'ztj',
                    'password':12345,
                    'email':'ztj@126.com',
                    'safequestion':'我的电话号码',
                    'safeanswer':'13805728737',
                    'sex':1,
                    }
        ret = self.user.register(new_data)
        self.assertEqual(True, ret[2])
        self.assertEqual(new_data['username'], ret[0])
        # 重复的用户名
        error_data = {'username':'zdz',
                    'password':12345,
                    'email':'ztj@126.com',
                    'safequestion':'我的电话号码',
                    'safeanswer':'13805728737',
                    'sex':1,
                    }
        self.assertEqual(False, self.user.register(error_data))
        # 重复的email
        error_data = {'username':'ztj',
                    'password':12345,
                    'email':'ztj@126.com',
                    'safequestion':'我的电话号码',
                    'safeanswer':'13805728737',
                    'sex':1,
                    }
        self.assertEqual(False, self.user.register(error_data))
        # 缺少某一项
        error_data = {'username':'ztj',
                    'password':12345,
                    'email':'ztj@126.com',
                    'safequestion':'我的电话号码',
                    'safeanswer':'13805728737',
                    }
        self.assertRaises(KeyError, self.user.register, error_data)
#        ret = self.user.register({})
#        self.assertEqual(ret, False)
        
    # 登录
    def test_login(self):
        # Exception
        self.assertRaises(KeyError, self.user.login, {})
        # Right
        data = {'username':self.baseuser.username,
                'password':self.baseuser.password}
        ret = self.user.login(data)
        self.assertEqual(True, ret[2])
        self.assertEqual(self.baseuser.username, ret[0])
        # password Wrong
        data = {'username':self.baseuser.username,
                'password':self.baseuser.password + '123'}
        self.assertEqual(False, self.user.login(data))
        # username Wrong
        data = {'username':self.baseuser.username + '123',
                'password':self.baseuser.password}
        self.assertEqual(False, self.user.login(data))

        
    # 返回注册安全问题
    def test_get_safequestion(self):
        # Exception
        self.assertRaises(KeyError, self.user.get_safequestion, {})
        # Right data
        data = {'username':self.baseuser.username}
        self.assertEqual(self.baseuser.safequestion, self.user.get_safequestion(data))
        # Wrong data
        data = {'username':self.baseuser.username + '123'}
        self.assertEqual(False, self.user.get_safequestion(data))

    
    # 验证安全问题答案
    def test_is_answer_right(self):
        # Exception
        self.assertRaises(KeyError, self.user.is_answer_right, {})
        # Right data
        data = {'username':self.baseuser.username,
                'safequestion':self.baseuser.safequestion,
                'safeanswer':self.baseuser.safeanswer}
        self.assertEqual(True, self.user.is_answer_right(data))
        # Wrong username
        data = {'username':self.baseuser.username + '123',
                'safequestion':self.baseuser.safequestion,
                'safeanswer':self.baseuser.safeanswer}
        self.assertEqual(False, self.user.is_answer_right(data))
        # Wrong safequestion
        data = {'username':self.baseuser.username + '123',
                'safequestion':self.baseuser.safequestion + 'qwe',
                'safeanswer':self.baseuser.safeanswer}
        self.assertEqual(False, self.user.is_answer_right(data))
        # Wrong safeanswer
        data = {'username':self.baseuser.username,
                'safequestion':self.baseuser.safequestion,
                'safeanswer':self.baseuser.safeanswer + 'qwe'}
        self.assertEqual(False, self.user.is_answer_right(data))
        

    
    # 密码重设
    def test_reset_key(self):
        self.assertRaises(KeyError, self.user.reset_key, {})
        # Right 
        new_password = self.baseuser.password + '123'
        data = {'username':self.baseuser.username,
                'password':new_password}
        self.assertEqual(True, self.user.reset_key(data))
        data = {'username':self.baseuser.username,
                'password':new_password}
        ret = self.user.login(data)
        self.assertEqual(True, ret[2])
        self.assertEqual(self.baseuser.username, ret[0])
        # Wrong username
        new_password = self.baseuser.password + '123'
        data = {'username':self.baseuser.username + '123',
                'password':new_password}
        self.assertEqual(False, self.user.reset_key(data))
        

        
    # 参加活动
    def test_attend_activity(self):
        self.assertRaises(KeyError, self.user.attend_activity, {})
        # Right
        data = {'username':self.baseuser.username,
                'activityid':self.acti.pk,
                }
        self.assertEqual(True, self.user.attend_activity(data))
        self.assertEqual(self.baseuser, Activity.objects.get(pk=self.acti.pk).userlist.get(pk=self.baseuser.pk))
        # Wrong username
        data = {'username':self.baseuser.username + '123',
                'activityid':self.acti.pk,
                }
        self.assertEqual(False, self.user.attend_activity(data))
        # Wrong activityid
        data = {'username':self.baseuser.username + '123',
                'activityid':self.acti.pk + 1,
                }
        self.assertEqual(False, self.user.attend_activity(data))
        


class ActivityTest(TestCase):
    def setUp(self):
        self.acti = Activity()
        self.baseuser = User(username='zdz',
                             password='12345',
                             email='zdz@126.com',
                             type=1,
                             safequestion='我的手机号码',
                             safeanswer='13805728737',
                             sex=0,
                             )
        self.baseuser.save()
        # Activity a
        self.acti_a = Activity(title='腐败一日游',
                             content='逛西湖一天',
                             releasetime=getDatetime('2012-7-1 12:00:00'),
                             date=getNow(),
                             owner=self.baseuser,
                             place='杭州西湖',
                               )
        self.acti_a.save()
        self.acti_a.releasetime= getDatetime('2012-7-1 12:00:00')
        self.acti_a.save()
        # Activity b
        self.acti_b = Activity(title='腐败二日游',
                             content='逛西湖二天',
                             releasetime=getDatetime('2012-9-1 12:00:00'),
                             date=getNow(),
                             owner=self.baseuser,
                             place='杭州西湖',
                               )
        self.acti_b.save()
        self.acti_b.releasetime= getDatetime('2012-9-1 12:00:00')
        self.acti_b.save()
        # Activity c
        self.acti_c = Activity(title='腐败三日游',
                             content='逛西湖三天',
                             releasetime=getDatetime('2012-9-1 12:00:00'),
                             date=getNow(),
                             owner=self.baseuser,
                             place='杭州西湖',
                               )
        
    def test_retrieve_activity(self):
        data = {'lasttime':getNow()}
        self.assertEqual(self.acti_b, Activity().retrieve_activity(data)[0])
        data = {'lasttime':getDatetime('2012-7-1 12:00:00')}
        self.assertEqual(self.acti_a, Activity().retrieve_activity(data)[0])
        self.assertEqual(self.acti_b, Activity().retrieve_activity(data)[1])
        
    def test_add_activity(self):
        data = {'title':self.acti_c.title,
                'uid':self.acti_c.owner.pk,
                'date':self.acti_c.date,
                'content':self.acti_c.content,
                'place':self.acti_c.content,
                }
        self.assertEquals(True,Activity().add_activity(data))
class NewsTest(TestCase):
    def setUp(self):
        self.news = News()
        # set up NewsType
        newstype = NewsType()
        newstype.title = '职场动态'
        newstype.save()
        newstype = NewsType()
        newstype.title = '主题学习'
        newstype.save()
        
        # set up News
        # 1
        self.news_a = News(keyword='领导力',
                           title='黄光裕近况：狱中操控国美人事调整',
                           type=NewsType.objects.get(title='职场动态'),
                           releasetime=getDatetime('2012-7-1 00:00:00'),
                           content=' 黄光裕近况怎么样？如今过去快三年了，大家最关心的还是黄光裕近况',
                           )
        self.news_a.save()
        # 2
        self.news_b = News(keyword='经理人学习',
                           title='黄光裕以内幕交易罪',
                           type=NewsType.objects.get(title='职场动态'),
                           releasetime=getDatetime('2012-9-1 00:00:00'),
                           content='根据国美方面提供的资料显示，此次调整中重点包括了新设&ldquo;高级副总裁&',
                           )
        self.news_b.save()
        # 3
        self.news_c = News(keyword='动态',
                           title='据国美方面提供的资料显示',
                           type=NewsType.objects.get(title='职场动态'),
                           releasetime=getDatetime('2012-9-1 00:00:00'),
                           content='根据国美方面提供的资料显示，此次调整中重点包括了新设&ldquo;高级副总裁&',
                           )
        self.news_c.save()
        # 4
        self.news_d = News(keyword='动态',
                           title='此次调整中重点',
                           type=NewsType.objects.get(title='主题学习'),
                           releasetime=getDatetime('2012-9-1 00:00:00'),
                           content='根据国美方面提供的资料显示，此次调整中重点包括了新设&ldquo;高级副总裁&',
                           )
        self.news_d.save()
        
    
    def test_retrieve_news(self):
        data = {'typeid':1,
                'lasttime':getNow(),
                }
        self.assertEqual(self.news_b, News().retrieve_news(data)[0])
        news_one = News().retrieve_news(data)[0]
        dict={'keyword':news_one.keyword,"title":news_one.title,"type":news_one.title}
        self.assertEqual(self.news_c, News().retrieve_news(data)[1])
        data = {'typeid':2,
                'lasttime':getNow(),
                }
        #print data
        self.assertEqual(self.news_d, News().retrieve_news(data)[0])


class SimpleTest(TestCase):
    def test_basic_addition(self):
        """
        Tests that 1 + 1 always equals 2.
        """
        self.assertEqual(1 + 1, 2)
